import 'package:flutter/material.dart';
import 'package:flutter_with_bmap/consts/msg_type.dart';
import 'package:flutter_with_bmap/main.dart';
import 'package:flutter_with_bmap/util/my_icon.dart';
import 'package:flutter_with_bmap/widgets/event_icon.dart';

class ToastMsg {
  String msg;
  IconData? iconData;
  ToastMsg(this.msg, this.iconData);
}

class NotifyMsg {
  String title;
  String msg;
  MsgType? msgType;
  NotifyMsg(this.title, this.msg, this.msgType);
}

class OverLayUtil {
  static final OverlayState? _overlay = navigatorKey.currentState?.overlay;
  static final List<ToastMsg> _toasts = [];
  static final List<NotifyMsg> _notifies = [];
  static OverlayEntry? _notifyOverlayEntry;
  static OverlayEntry? _toastOverlayEntry;

  static Widget _toastBuilder(BuildContext context) {
    return Center(
        child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: _toasts
                .map<Widget>((e) => Container(
                      margin: const EdgeInsets.only(bottom: 5),
                      padding: const EdgeInsets.fromLTRB(15, 10, 15, 10),
                      decoration: const BoxDecoration(
                          color: Color.fromRGBO(0, 0, 0, .7),
                          borderRadius: BorderRadius.all(Radius.circular(8))),
                      child: Column(children: [
                        Container(
                            margin: const EdgeInsets.only(bottom: 2),
                            child: const Icon(
                              MyIcon.warning,
                              size: 30,
                              color: Colors.white,
                            )),
                        const Text("msg",
                            style: TextStyle(
                                fontSize: 18,
                                color: Colors.white,
                                decoration: TextDecoration.none))
                      ]),
                    ))
                .toList()));
  }

  static void showToast(String msg,
      {Duration duration = const Duration(seconds: 2), IconData? icon}) async {
    ToastMsg toastMsg = ToastMsg(msg, icon);
    _toasts.add(toastMsg);
    if (_toastOverlayEntry == null) {
      _toastOverlayEntry = OverlayEntry(builder: _toastBuilder);
      _overlay!.insert(_toastOverlayEntry!);
    } else {
      _toastOverlayEntry!.markNeedsBuild();
    }
    Future.delayed(duration).then((value) {
      _toasts.remove(toastMsg);
      _toastOverlayEntry!.markNeedsBuild();
    });
  }

  static Widget _notifyBuilder(BuildContext context) {
    return Positioned(
        right: 10,
        top: 10,
        child: SizedBox(
          width: 400,
          height: _notifies.length * 150,
          child: ListView(
            shrinkWrap: true,
            children: _notifies
                .map<Widget>((notifyMsg) => Container(
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(5)),
                    width: 400,
                    margin: const EdgeInsets.only(bottom: 10),
                    child: Row(children: [
                      EventIcon(notifyMsg.msgType!),
                      Expanded(
                          child: Column(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Container(
                              margin: const EdgeInsets.only(bottom: 5),
                              child: Text(notifyMsg.title,
                                  style: const TextStyle(
                                      fontSize: 18,
                                      color: Colors.black,
                                      decoration: TextDecoration.none))),
                          Text(notifyMsg.msg,
                              style: const TextStyle(
                                  fontSize: 14,
                                  color: Colors.black54,
                                  decoration: TextDecoration.none))
                        ],
                      ))
                    ])))
                .toList(),
          ),
        ));
  }

  static void showNotify(String title, String msg,
      {Duration duration = const Duration(seconds: 2), MsgType? msgType}) {
    NotifyMsg m = NotifyMsg(title, msg, msgType);
    _notifies.add(m);
    if (_notifyOverlayEntry == null) {
      _notifyOverlayEntry = OverlayEntry(builder: _notifyBuilder);
      _overlay!.insert(_notifyOverlayEntry!);
    } else {
      _notifyOverlayEntry!.markNeedsBuild();
    }
    Future.delayed(duration).then((value) {
      _notifies.remove(m);
      _notifyOverlayEntry!.markNeedsBuild();
    });
  }
}
